package com.xneure.function.mode;

import java.util.ArrayList;
import java.util.List;

import com.xneure.code.exception.FunctionException;
import com.xneure.function.enums.FunctionType;
import com.xneure.function.enums.ParamType;
import com.xneure.utils.bean.GUID;

public abstract class FunctionBase implements Function {
	protected String name;
	private FunctionType type;
	private List<Param> params;

	public FunctionBase(FunctionType type) {
		this.type = type;
		this.name = this.getClass().getSimpleName();
		params = new ArrayList<Param>(10);
		initParam();
	}

	/**
	 * 参数为字符串类型，初始值为空，非必填
	 */
	protected void appendParam(String name) {
		appendParam(name, "");
	}

	/**
	 * 参数初始值为 null，非必填
	 */
	protected void appendParam(String name, ParamType type) {
		appendParam(name, type, null);
	}

	/**
	 * 字符串类型，非必填
	 */
	protected void appendParam(String name, Object initValue) {
		appendParam(name, ParamType.STRING, initValue);
	}

	/**
	 * 非必填
	 */
	protected void appendParam(String name, ParamType type, Object initValue) {
		appendParam(name, ParamType.STRING, initValue, false);
	}

	/**
	 * 添加参数
	 * 
	 * @param name
	 * @param type
	 * @param initValue
	 * @param isIgnore
	 */
	protected void appendParam(String name, ParamType type, Object initValue, boolean isIgnore) {
		Param par = new Param(name, type, initValue, params.size(), isIgnore);
		params.add(par);
	}

	@Override
	public void setParam(int index, Object value) {
		setParamValue(index, value);
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public FunctionType getType() {
		return type;
	}

	@Override
	public abstract String getDescription();

	@Override
	public String[] getParamNames() {
		String[] names = new String[params.size()];
		for (int i = 0; i < names.length; i++) {
			names[i] = params.get(i).getName();
		}
		return names;
	}

	@Override
	public Param getParam(int index) {
		checkParam(index);
		return params.get(index);
	}

	public void setParamDescription(String paramName, String description) {
		checkParam(paramName).description = description;
	}

	public void setParamValue(int index, Object val) {
		checkParam(index).setValue(val);
	}

	public void setParamValue(String paramName, Object val) {
		checkParam(paramName).setValue(val);
	}

	protected void setParamDescription(int index, String description) {
		checkParam(index).description = description;
		;
	}

	@Override
	public Param getParam(String paramName) {
		return checkParam(paramName);
	}

	protected Param checkParam(String paramName) {
		for (Param param : params) {
			if (param.getName().equals(paramName)) {
				return param;
			}
		}
		throw new FunctionException(paramName.concat(" 参数无效。"));
	}

	protected Param checkParam(int index) {
		if (index < 0 || index >= params.size()) {
			throw new FunctionException("参数索引值 " + index + " 无效。");
		} else {
			return params.get(index);
		}
	}

	@Override
	public int getParamCount() {
		return params.size();
	}

	protected abstract void initParam();

	@Override
	public GUID getFunctionFlag() {
		return GUID.randomID();
	}

	@Override
	public String getGroupFlag() {
		StringBuilder sb = new StringBuilder(this.name);
		// for(Entry<String,Param> e:params.entrySet()){
		// Param val = e.getValue();
		// if(null != val.isIgnore){
		// sb.append(e.getKey());
		// }
		// }
		return sb.toString();
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer(name);
		sb.append(":").append(type.getTitle()).append("@");
		for (Param e : params) {
			sb.append(e.name).append(",");
		}
		sb.setLength(sb.length());
		return sb.toString();
	}

}
